#!/usr/bin/python
import matplotlib.pyplot as plt
import random
import time
thetastd=50
te=2

def mean(todolist):
	return sum(todolist)/len(todolist)

def geterror(pacNumber,theta,nt):
	yi=[]
	for i in range(0,pacNumber):
		xi=random.gauss(0,11.1)
		yi.append(nt+theta+xi)
	thetaEstm=mean(yi)-nt
	error=theta-thetaEstm
	return error

def geterrorDMTS(pacNumber,theta,nt):
	error=random.gauss(0,11.1)
	return error

def simu(sims,maxpac,theta):
	error=[]
	avg=[]
	for pac in range(1,maxpac+1):
		for sim in range(1,sims):
			error.append(geterror(pac,theta,te))
		avg.append(abs(mean(error)))
		error=[]
		print "pac",pac,"done!"
	return avg

def simuDMTS(sims,maxpac):
	error=[]
	avg=[]
	for pac in range(1,maxpac+1):
		for sim in range(1,sims):
			error.append(geterrorDMTS(pac,thetastd,te))
		avg.append(abs(mean(error)))
		error=[]
		print "pac",pac,"done!"
	return avg

result=[]

#result1=[]
for t in range(0,100):
	avg=simu(1000,15,thetastd)
#avg1=simuDMTS(1000,15)
	print "*********************************simu ",t," done!"
	print time.asctime()
	result.append(avg)
#	result1.append(avg1)
	avg=[]
#	avg1=[]
maxerror=[0]*15

#maxerrorDMTS=[0]*15
for i in range(len(result)):
	for t in range(15):
		if result[i][t]>maxerror[t]:
			maxerror[t]=result[i][t]
#		if result1[i][t]>maxerrorDMTS[t]:
#			maxerrorDMTS[t]=result1[i][t]

plt.plot(range(1,16),maxerror,"r")
plt.xlabel("packets")
plt.ylabel("error(us)")
plt.xticks(range(1,16,1))
plt.show()
